import sys
import os
import docx
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

def get_doc_list(docs_path):
    files = []
    # 从文件路径获取文件列表,并剔除非doc格式的文件
    for file in os.listdir(docs_path):
        if file.endswith('.doc') or file.endswith('.docx'):
            files.append(docs_path + "/" + file)
    return files

def replace_str(files_list):
    # 替换str1
    old_str1 = "Please note we provide an open source software notice along with this product and/or this product firmware (in the following just “this product”). The open source software licenses are granted by the respective right holders. And the open source licenses prevail all other license information with regard to the respective open source software contained in the product, including but not limited to End User Software Licensing Agreement. This notice is provided on behalf of openEuler project which is incubated and hosted by the OpenAtom Foundation, and may have provided this product to you in your local country."
    new_str1 = "The open source software notice for the open source software used in this openEuler distribution (“openEuler distribution”) is provided in this document. The open source software licenses are granted by the respective right holders. With regard to the respective open source software contained in the openEuler distribution, the applicable open source software license will prevail all other license agreement, including but not limited to the openEuler Software License, in case of any conflicts."

    # 替换str2
    old_str2 = "The open source software in this product is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the applicable licenses for more details."
    new_str2 = "THE OPEN SOURCE SOFTWARE IN THIS OPENEULER DISTRIBUTION IS PROVIDED ON AN \"AS IS\" BASIS, WITHOUT ANY WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE."
    surplus_str = "See the applicable licenses for more details."

    # 替换str3
    new_str3 = "This openEuler distribution may contain certain software whose rights holders license it on the terms of the GNU General Public License, version 2 (GPLv2) or other open source software licenses which require us to release corresponding source code. We will provide you and any third party with corresponding source code required under applicable open source software license through the repository: https://gitee.com/src-openeuler/.  You can access and obtain corresponding source code by searching the aforementioned repository using package name and tag."

    # 替换str4
    old_str4 = "This offer is valid for three years from the moment we distributed the product or firmware ."
    new_str4 = "THIS OFFER IS VALID FOR THREE YEARS FROM THE MOMENT WE DISTRIBUTED THIS OPENEULER DISTRIBUTION ."

    for file in files_list:
        # 创建 Document 对象，相当于打开一个 word 文档
        print(file)
        doc = docx.Document(file)
        paragraphs_len = len(doc.paragraphs)
        paragraphs = doc.paragraphs
        for index in range(0, paragraphs_len):
            if old_str1 in paragraphs[index].text:
                paragraphs[index].text = paragraphs[index].text.replace(old_str1, new_str1)
                paragraphs[index].style.font.name = 'Arial'

            if old_str2 in paragraphs[index].text:
                paragraphs[index].text = paragraphs[index].text.replace(old_str2, '')
                paragraphs[index].style.font.name = 'Arial'
                paragraphs[index].add_run(new_str2 + "\n" + surplus_str).bold = True

            if "Written Offer" in paragraphs[index].text:
                paragraphs[index + 1].clear()
                paragraphs[index + 1].text = new_str3
                paragraphs[index + 1].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
                paragraphs[index + 2].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
                paragraphs[index + 3].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY

            if old_str4 in paragraphs[index].text:
                paragraphs[index].text = paragraphs[index].text.replace(old_str4, '')
                runs = paragraphs[index].add_run(new_str4)
                runs.font.name = 'Times New Roman'
                runs.bold = True

        doc.save(file)
def main():
    if len(sys.argv) != 2:
        print("Error: Wrong number of parameters")
        sys.exit(1)
    docs_path = sys.argv[1]
    files_list = get_doc_list(docs_path)
    replace_str(files_list)

if __name__=="__main__":
    main()
